package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class DishServiceImpl implements DishService {

    private final DishMapper dishMapper;

    private final FlavorMapper flavorMapper;

    private final SetmealMapper setmealMapper;

    private final SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DishDTO dishDTO) {
        Dish dish = BeanUtil.copyProperties(dishDTO, Dish.class);
        dish.setStatus(StatusConstant.ENABLE);
        //将菜品添加到数据库中
        dishMapper.save(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        //为每个口味添加菜品id
        flavors.forEach(flavor -> flavor.setDishId(dish.getId()));

        //将口味添加到数据库中
        flavorMapper.saveDishFlavors(flavors);
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<Dish> page=dishMapper.page(dishPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Long> ids) {
        //可以一次删除多个
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                //起售中的菜品不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //被套餐关联的菜品不能删除
        List<Long> setmealIds = setmealMapper.getSetmealIdByDishId(ids);
        if(setmealIds!=null&& !setmealIds.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        for (Long id : ids) {
            //删除菜品后关联的口味也要删除
            flavorMapper.deleteByDishId(id);
            //删除菜品信息
            dishMapper.deleteBatch(id);
        }
    }

    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> flavors = flavorMapper.getByDishId(id);
        DishVO dishVO = BeanUtil.copyProperties(dish, DishVO.class);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public void status(Integer status, Long dishId) {
        Dish dish = Dish.builder()
                .id(dishId)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DishDTO dishDTO) {
        Dish dish = BeanUtil.copyProperties(dishDTO, Dish.class);
        Long id = dish.getId();
        //删除原来的相关口味
        flavorMapper.deleteByDishId(id);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(id));
        //插入新的口味
        flavorMapper.saveDishFlavors(flavors);
        //修改信息
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> getByCategoryId(Category category) {
        List<DishVO> dishVOList = dishMapper.getByCategoryId(category);
        for (DishVO dishVO : dishVOList) {
            List<DishFlavor> byDishId = flavorMapper.getByDishId(dishVO.getId());
            dishVO.setFlavors(byDishId);
        }
        return dishVOList;
    }

    @Override
    public List<DishItemVO> getDishItem(Long id) {
        return  setmealDishMapper.getDishItemBySetmealId(id);
    }
}
